package cn.tedu._03vehicle.controller;

import cn.tedu._03vehicle.base.response.JsonResult;
import cn.tedu._03vehicle.base.response.StatusCode;
import cn.tedu._03vehicle.mapper.VehicleMapper;
import cn.tedu._03vehicle.pojo.datatransferobject.VehicleAddParam;
import cn.tedu._03vehicle.pojo.datatransferobject.VehicleList;
import cn.tedu._03vehicle.pojo.entity.Vehicle;
import cn.tedu._03vehicle.pojo.valueobject.UserVO;
import cn.tedu._03vehicle.pojo.valueobject.VehicleVO;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpSession;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@Validated//非封装类参数校验
@Tag(name = "02-车辆管理模块")
@RestController
@RequestMapping("v1/vehicle")
@Slf4j
public class VehicleController {
    @Autowired
    private VehicleMapper vehicleMapper;

    @ApiOperationSupport(order = 10)
    @Operation(summary = "01-车辆添加")
    @PostMapping("/add")
    public JsonResult addVehicleParam(@RequestBody VehicleAddParam vehicleAddParam, HttpSession session) {
        log.debug("vehicleAddParam:" + vehicleAddParam);
        UserVO userVO = (UserVO) session.getAttribute("user");
        //拦截器发力了，所以下面注释掉
//        if (userVO == null) {
//            return new JsonResult(StatusCode.OPERATION_NOT_LOGIN);
//        } else {
        Vehicle vehicle = new Vehicle();
        BeanUtils.copyProperties(vehicleAddParam, vehicle);
        vehicle.setCreateTime(new Date());
        vehicle.setUpdateTime(new Date());
        vehicle.setUserId(userVO.getId());
        log.debug("vehicle:" + vehicle);
        int num = vehicleMapper.addVehicle(vehicle);
        if (num > 0) {
            return JsonResult.ok();
        }
        return new JsonResult(StatusCode.OPERATION_FAILED);
    }



    @GetMapping("/list")
    @Operation(summary = "02-车辆列表")
    @ApiOperationSupport(order = 20)
    @Parameters(value = {
            @Parameter(name = "brand", description = "车辆品牌"),
            @Parameter(name = "license", description = "车牌号"),
            @Parameter(name = "vehicleList", hidden = true)
    })
    public JsonResult list(VehicleList vehicleList, HttpSession session) {
        log.debug("vehicleList = " + vehicleList);
        UserVO userVO = (UserVO) session.getAttribute("user");
        //未登录状态
//        if (userVO == null) {
//            return new JsonResult(StatusCode.OPERATION_NOT_LOGIN);
//        }
        //登录状态
        //SELECT ... FROM vehicle WHERE brand=? AND license=? AND user_id=?
        String brand = vehicleList.getBrand();
        String license = vehicleList.getLicense();
        Long userId = userVO.getId();
        List<VehicleVO> vehicleVOS = vehicleMapper.queryVehicle(brand, license, userId);
        return JsonResult.ok(vehicleVOS);
    }

    @PostMapping("del/{id}")
    @ApiOperationSupport(order = 30)
    @Operation(summary = "03-删除车辆")
    public JsonResult deleteVehicle(@Range(min = 1, max = 500, message = "车辆id必须在1到500之间") @PathVariable Long id, HttpSession session) {
        if (id < 0) {
            throw new IllegalArgumentException("车辆编号不能小于0");
        }
        log.debug("id:" + id);
        UserVO userVO = (UserVO) session.getAttribute("user");
//        if (userVO == null){
//            return new JsonResult(StatusCode.OPERATION_NOT_LOGIN);
//        }
        Long userId = userVO.getId();
        vehicleMapper.delVehicle(id, userId);
        return JsonResult.ok();
    }
}